package cn.iocoder.yudao.module.infra.dal.mysql.category;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.infra.controller.admin.category.vo.CategoryListVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.category.CategoryDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * TODO
 *
 * @author sun
 * @date 15:21 2023/12/4
 **/
@Mapper
public interface CategoryMapper extends BaseMapperX<CategoryDO> {

    /**
     * 根据Code名称查询分类
     * @param code
     * @return
     */
    @Select("select * from infra_category where parent_id = (select id from infra_category where code = #{code} order by sort) ")
    List<CategoryDO> selectCategoryByCode(String code);

    default List<CategoryDO> selectList(CategoryListVO reqVO) {
        LambdaQueryWrapperX<CategoryDO> categoryDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        categoryDOLambdaQueryWrapperX
                .likeIfPresent(CategoryDO::getName, reqVO.getName())
                .eqIfPresent(CategoryDO::getStatus, reqVO.getStatus());
        if (reqVO.getParentId() != null) {
            categoryDOLambdaQueryWrapperX.eq(CategoryDO::getParentId, reqVO.getParentId());
        }
        return selectList(categoryDOLambdaQueryWrapperX);
    }

    default List<CategoryDO> selectSimpleList(CategoryListVO reqVO) {
        return selectList(new LambdaQueryWrapperX<CategoryDO>()
                .eqIfPresent(CategoryDO::getStatus, reqVO.getStatus())
        );
    }
}
